package com.softwinner.bt.Fragment;

import com.softwinner.bt.MainActivity;
import com.softwinner.bt.R;
import com.softwinner.bt.R.id;
import com.softwinner.bt.R.layout;
import com.softwinner.bt.R.string;
import com.softwinner.bt.policy.BTManager;
import com.softwinner.bt.service.BtService;
import com.softwinner.bt.util.BTLog;
import com.softwinner.bt.util.Command;
import com.softwinner.bt.util.ContactsQueryUtil;
import com.softwinner.bt.util.GpioHelper;
import com.softwinner.bt.util.Utils;
import android.util.Log;
import android.app.Fragment;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainFragment extends Fragment implements OnClickListener {

	private MainActivity mActivity;
	// private IBtService mBtService;

	private TextView mKeyboard;
	private TextView mContact;
	private TextView mPairList;
	private TextView mCallLog;
	private TextView mPower;
	private TextView mRePair;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.bt_main_fragment, container,
				false);
		mKeyboard = (TextView) view.findViewById(R.id.main_tv_keyboard);
		mContact = (TextView) view.findViewById(R.id.main_tv_contact);
		mPairList = (TextView) view.findViewById(R.id.main_tv_pair);
		mCallLog = (TextView) view.findViewById(R.id.main_tv_calllog);
		mPower = (TextView) view.findViewById(R.id.main_tv_power);
		mRePair = (TextView) view.findViewById(R.id.main_tv_repair);

		mKeyboard.setOnClickListener(this);
		mContact.setOnClickListener(this);
		mPairList.setOnClickListener(this);
		mCallLog.setOnClickListener(this);
		mPower.setOnClickListener(this);
		mRePair.setOnClickListener(this);

		return view;
	}

	private static final int MSG_CLOSESPK = 0;
	private static final int MSG_OPENSPK = 1;
	private static final int MSG_OPENBT = 2;
	private static final int MSG_CLOSEBT = 3;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case MSG_CLOSESPK:
				//suzhen 1.FM´ò¿ªÊ±£¬°´À¶ÑÀ¿ª¹Ø°´Å¥ FMºÍ»úÆ÷Í¬Ê±³öÉùÎÊÌâ	
				//GpioHelper.closeSPK();
				//mHandler.sendEmptyMessageDelayed(MSG_OPENSPK, 1000);
				break;
			case MSG_OPENSPK:
				Log.i("suzhen","bt GpioHelper.openSPK()------->");
				GpioHelper.openSPK();
				break;
			case MSG_OPENBT:
				GpioHelper.openBtPower();
				updateStatus();
				//suzhen 2.FM´ò¿ªÊ±£¬°´À¶ÑÀ¿ª¹Ø°´Å¥ FMºÍ»úÆ÷Í¬Ê±³öÉùÎÊÌâ
				//mHandler.sendEmptyMessageDelayed(MSG_CLOSESPK,3000);
				break;
			case MSG_CLOSEBT:
				GpioHelper.closeBtPower();
				updateStatus();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mActivity = (MainActivity) this.getActivity();
		updateStatus();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.main_tv_keyboard:
			mActivity.SetFragment(mActivity.TYPE_KEYBOARD);
			break;
		case R.id.main_tv_contact:
			mActivity.SetFragment(mActivity.TYPE_CONTACT);
			break;
		case R.id.main_tv_pair:
			mActivity.SetFragment(mActivity.TYPE_PAIRLIST);
			break;
		case R.id.main_tv_calllog:
			mActivity.SetFragment(mActivity.TYPE_CALLLOG);
			break;
		case R.id.main_tv_power:
			if (GpioHelper.readBtStatus() == GpioHelper.BT_OPEN) {
				//GpioHelper.closeBtPower();
				if (GpioHelper.IsSpkOpen()) {
					//mHandler.sendEmptyMessage(MSG_CLOSESPK);
				}
				mHandler.sendEmptyMessageDelayed(MSG_CLOSEBT,200);
				BtService.mBtPairName = "";
				BtService.mBtPairAdd = "";
				Utils.sendBroadcast(mActivity, Utils.ACTION_BTSTATE_CHANGED);
			} else {
				//GpioHelper.openBtPower();				
				mHandler.sendEmptyMessage(MSG_OPENBT);
			}			
			break;
		case R.id.main_tv_repair:
			BTManager.getInstance().sendCommand(Command.SEND_DIS_CONNECTED);
			break;
		}
	}

	public void updateStatus() {
		if (GpioHelper.readBtStatus() == GpioHelper.BT_CLOSE) {
			mPower.setText(R.string.title_open_bt);
			mRePair.setEnabled(false);
			mActivity.updateStatus();
		} else {
			mPower.setText(R.string.title_close_bt);
			mRePair.setEnabled(true);
			mActivity.updateStatus();
		}
	}
}
